package com.fau.cm.action.index;

import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.cm.action.PublicAction;
import com.fau.cm.entity.MedicalAdmin;
import com.fau.cm.entity.MedicalCallRecord;
import com.fau.cm.util.FileNameEncoderUtil;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc  话单记录管理Action
 * @author yangzj
 * @datetime 2014年10月19日,下午4:29:05
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class CallRecordsManageAction extends PublicAction implements ModelDriven<MedicalCallRecord>{
	
	private static final long serialVersionUID = 1611864591524234673L;
	
	private MedicalCallRecord callRecord = new MedicalCallRecord();

	/**
	 * @desc 跳转到话单记录管理页面
	 * @return SUCCESS
	 * @author yangzj
	 * @datetime 2014年10月19日,下午4:35:42
	 */
	@Action(value = "call_records_init",results = {@Result(location = "call_records_init.jsp")})
	public String init() {
		return SUCCESS;
	}
	
	/**
	 * @desc 分页查询话单记录
	 * @return null
	 * @author liy
	 * @datetime 2014年12月11日,上午10:18:16
	 */
	@Action(value = "call_records_list")
	public String page(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = ServletActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		pageBean = this.baseAllService.getBillingManagerService().findCallRecordByPage(map, this.callRecord, admin.getEnterpriseId());
		map.clear();
		map.put("total", pageBean.getTotalRow());
		map.put("data", pageBean.getResult());
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 计算话费合计
	 * @return null
	 * @author liy
	 * @datetime 2014年12月11日,上午11:19:30
	 */
	@Action(value = "call_records_sum")
	public String sum(){
		String sum = this.baseAllService.getBillingManagerService().sumCallFee(this.callRecord);
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = new HashMap<String, Object>();
		map.put("sum", sum);
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 话单记录-导出当前
	 * @return null
	 * @author liy
	 * @datetime 2014年12月29日,下午5:33:29
	 */
	@Action(value = "call_records_export_all")
	public String allExport() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"话单记录导出.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getBillingManagerService().exportCallRecordAll(this.callRecord);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 话单记录-导出全部
	 * @return 
	 * @author liy
	 * @datetime 2014年12月29日,下午8:32:18
	 */
	@Action(value = "call_records_export_current")
	public String currentExport() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"话单记录导出.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getBillingManagerService().exportCallRecordCurrent(this.callRecord.getId());
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	

	@Override
	public MedicalCallRecord getModel() {
		return callRecord;
	}
}
